﻿using UnityEngine;
using System.Collections.Generic;
using System;

public class ObjectPool<T>
{
    protected Queue<T> m_Pool;
    System.Type m_RealType;

    public ObjectPool(System.Type realType)
    {
        m_RealType = realType;
        m_Pool = new Queue<T>();
    }

    public T Get()
    {
        return Get<T>();
    }

    public TDerive Get<TDerive>()
        where TDerive : T
    {
        TDerive obj;
        if (m_Pool.Count > 0)
        {
            obj = (TDerive)m_Pool.Dequeue();
        }
        else
        {
            obj = (TDerive)Activator.CreateInstance(m_RealType);
        }
        return obj;
    }

    public void Put(T obj)
    {
        if (obj == null)
        {
            return;
        }
        m_Pool.Enqueue(obj);
    }
}